package org.dictionary;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * @author siarhei
 */
public class EntryViewActivity extends Activity {
    private Long rowId;

    private TextView originalText;
    private TextView translatedText;
    private TextView batch;

    private static final int DIALOG_DELETE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entry_view);

        originalText = (TextView) findViewById(R.id.original);
        translatedText = (TextView) findViewById(R.id.translated);
        batch = (TextView) findViewById(R.id.batch);

        Button editButton = (Button) findViewById(R.id.save);
        Button deleteButton = (Button) findViewById(R.id.delete);

        rowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            rowId = extras.getLong(DictionaryEntry._ID);
            updateFieldValues();
        }

        final Activity thisActivity = this;

        editButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putLong(DictionaryEntry._ID, rowId);

                Intent i = new Intent(thisActivity, EntryEditActivity.class);
                i.putExtras(bundle);
                startActivityForResult(i, 0);
            }

        });

        deleteButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                showDialog(DIALOG_DELETE);
            }
        });

    }

    private void updateFieldValues() {
        DictionaryEntry entry = DictionaryDao.getInstance(this).get(rowId);
        originalText.setText(entry.getOriginal());
        translatedText.setText(entry.getTranslation());
        batch.setText(entry.getBatch().toString());
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case DIALOG_DELETE:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to delete the card?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            DictionaryDao.getInstance(null).delete(rowId);
                            finish();
                        }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                   });
            dialog = builder.create();
            break;
        default:
            dialog = null;
        }
        return dialog;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        updateFieldValues();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.view_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.clear_batch:
                DictionaryEntry entry = DictionaryDao.getInstance(this).get(rowId);
                entry.setBatch(0);
                DictionaryDao.getInstance(this).save(entry);
                batch.setText("0");
                return true;

            default:
                return false;
        }
    }

}
